package com.hanyxx.thread.high;

/**
 * @author layman
 */
public class Consumer extends Thread {
    private BaoZi baozi;

    public Consumer(String name,BaoZi baozi){
        super(name);
        this.baozi = baozi;
    }

    @Override
    public void run() {
        while(true){
            synchronized (baozi){
                if(baozi.isFlag() == false){
                    try {
                        System.out.println("没有包子，等着生产包子");
                        System.out.println("--------------------------");
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //有包子，开吃
                System.out.println("吃货把 "+baozi.getXian()+" 包子吃完了，真香！");
                System.out.println("--------------------------");
                baozi.setFlag(false);
                try {
                    Thread.sleep(500L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                baozi.notify();
                }

        }
    }
}
